using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 向量的叉乘
/// </summary>
public class Lecture02_1 : MonoBehaviour
{
    // 两向量叉乘，得到一条与这两条向量垂直的向量(也可以说是法线)，使用右手螺旋定则

    public Transform pointA;
    public Transform pointB;
    public Transform pointC;
    public Transform target;

    private Vector3 abLine = Vector3.zero;
    private Vector3 bcLine = Vector3.zero;
    private Vector3 caLine = Vector3.zero;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    /// <summary>
    /// 检测玩家是否在范围内
    /// </summary>
    public void CheckTran()
    {
        Vector3 bT = GetPointToTarget(GetTarget(),GetBPoint());
        Vector3 crB = GetCross(GetAB(),bT);

        Vector3 cT = GetPointToTarget(GetTarget(),GetCPoint());
        Vector3 crC = GetCross(GetBC(),cT);

        Vector3 aT = GetPointToTarget(GetTarget(),GetAPoint());
        Vector3 crA = GetCross(GetCA(),aT);
        if (crB.y == crC.y && crB.y == crA.y && crA.y == crC.y)
        {
            Debug.LogError("角色在范围内");
        }
        else
        {
            Debug.LogError("角色在范围外");
        }
    }

    /// <summary>
    /// a->b 的向量
    /// </summary>
    /// <returns></returns>
    private Vector3 GetAB()
    {
        if (abLine != Vector3.zero)
        {
            return abLine;
        }
        abLine = pointB.position - pointA.position;

        return abLine;
    }

    /// <summary>
    /// b->c 的向量
    /// </summary>
    /// <returns></returns>
    private Vector3 GetBC()
    {
        if (bcLine != Vector3.zero)
        {
            return bcLine;
        }
        bcLine = pointC.position - pointB.position;

        return bcLine;
    }


    /// <summary>
    /// c->a 的向量
    /// </summary>
    /// <returns></returns>
    private Vector3 GetCA()
    {
        if (caLine != Vector3.zero)
        {
            return caLine;
        }
        caLine = pointA.position - pointC.position;

        return caLine;
    }


    /// <summary>
    /// 点到目标的向量
    /// </summary>
    /// <returns></returns>
    private Vector3 GetPointToTarget(Vector3 point,Vector3 target)
    {
        target.y = point.y;
        return target - point;
    }

    /// <summary>
    /// 获取两向量的叉乘
    /// </summary>
    /// <param name="p1"></param>
    /// <param name="p2"></param>
    /// <returns></returns>
    private Vector3 GetCross(Vector3 p1,Vector3 p2)
    {
        return Vector3.Cross(p1,p2).normalized;
    }

    /// <summary>
    /// 目标的v3
    /// </summary>
    /// <returns></returns>
    private Vector3 GetTarget()
    {
        return target.position;
    }

     private Vector3 GetAPoint()
    {
        return pointA.position;
    }

     private Vector3 GetBPoint()
    {
        return pointB.position;
    }

    private Vector3 GetCPoint()
    {
        return pointC.position;
    }


}
